//
//  CSFirstInPopController.m
//  CreateWithSuper
//
//  Created by chenjunbo on 2024/6/8.
//  Copyright © 2024 CS. All rights reserved.
//

#import "CSFirstInPopController.h"

@interface CSFirstInPopController ()<UITextViewDelegate>

@property (weak, nonatomic) IBOutlet UITextView *textView;

@property (weak, nonatomic) IBOutlet UILabel *textViewCountLabel;
@property (weak, nonatomic) IBOutlet UILabel *textViewPLabel;

@end

@implementation CSFirstInPopController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (IBAction)clearallClick:(id)sender {
    self.textView.text = @"";
    self.textViewCountLabel.text = @"0/102";
}

- (IBAction)createClick:(UIButton *)sender {
    self.rightBlock ? self.rightBlock(self.textView.text) : nil;
}

- (IBAction)tryluckClick:(UIButton *)sender {
    self.leftBlock ? self.leftBlock() : nil;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.textView endEditing:YES];
}

- (void)textViewDidChange:(UITextView *)textView {
    
    self.textViewCountLabel.text = [NSString stringWithFormat:@"%lu/102",(unsigned long)textView.text.length];
    
    if (textView.text.length > 0){
        self.textViewPLabel.hidden = YES;
    }else{
        self.textViewPLabel.hidden = NO;
    }
    
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    NSUInteger newTextLength = [textView.text length] + [text length] - range.length;
    if (newTextLength > 102) {
        return NO;
    }
    if ([text isEqualToString:@"\n"]) {
        [textView resignFirstResponder];
        return NO;
    }
    return YES;
}

@end
